package com.zj.core.ui

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.constraint.ConstraintLayout.LayoutParams.PARENT_ID
import android.text.InputFilter
import android.text.InputType
import android.text.method.DigitsKeyListener
import android.view.Gravity
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import com.zj.core.R
import org.jetbrains.anko.*
import org.jetbrains.anko.constraint.layout.constraintLayout
import org.jetbrains.anko.constraint.layout.guideline
import org.jetbrains.anko.constraint.layout.matchConstraint

class ConfigActivityUi(groupView: LinearLayout) : AnkoComponent<Context> {
    private val view: LinearLayout=groupView
    lateinit var ip_text_tv:EditText
    lateinit var port_text_tv:EditText
    lateinit var save_bu_click:Button


    override fun createView(ui: AnkoContext<Context>): View = with(view){
        constraintLayout{
            lparams(width= matchParent,height =matchParent)

            guideline {
                id = R.id.guideline_v_10
            }.lparams(width = matchConstraint, height = matchConstraint) {
                orientation = ConstraintLayout.LayoutParams.VERTICAL
                guidePercent = 0.1F
            }

            guideline {
                id = R.id.guideline_v_40
            }.lparams(width = matchConstraint, height = matchConstraint) {
                orientation = ConstraintLayout.LayoutParams.VERTICAL
                guidePercent = 0.4F
            }

            guideline {
                id = R.id.guideline_v_90
            }.lparams(width = matchConstraint, height = matchConstraint) {
                orientation = ConstraintLayout.LayoutParams.VERTICAL
                guidePercent = 0.9F
            }

            guideline {
                id = R.id.guideline_h_10
            }.lparams(width = matchConstraint, height = matchConstraint) {
                orientation = ConstraintLayout.LayoutParams.HORIZONTAL
                guidePercent = 0.1F
            }

            guideline {
                id = R.id.guideline_h_20
            }.lparams(width = matchConstraint, height = matchConstraint) {
                orientation = ConstraintLayout.LayoutParams.HORIZONTAL
                guidePercent = 0.2F
            }

            guideline {
                id = R.id.guideline_h_30
            }.lparams(width = matchConstraint, height = matchConstraint) {
                orientation = ConstraintLayout.LayoutParams.HORIZONTAL
                guidePercent = 0.3F
            }

            textView {
                text = "服务器ip"
                keyListener = DigitsKeyListener.getInstance("0123456789.")
                filters = arrayOf<InputFilter>(InputFilter.LengthFilter(15))
                gravity = Gravity.LEFT + Gravity.CENTER
            }.lparams(width = 0, height = 0) {
                topToBottom = R.id.guideline_h_10
                bottomToTop = R.id.guideline_h_20
                leftToRight = R.id.guideline_v_10
                rightToLeft = R.id.guideline_v_40
            }

            textView {
                text = "服务器端口"
                gravity = Gravity.LEFT + Gravity.CENTER
            }.lparams(width = 0, height = 0) {
                topToBottom = R.id.guideline_h_20
                bottomToTop = R.id.guideline_h_30
                leftToRight = R.id.guideline_v_10
                rightToLeft = R.id.guideline_v_40
            }

            ip_text_tv=editText {
                hint = "请输入ip"
                maxLines = 1
                id = R.id.ip_text_tv
                keyListener = DigitsKeyListener.getInstance("0123456789.")
                filters = arrayOf<InputFilter>(InputFilter.LengthFilter(15))
                gravity = Gravity.LEFT + Gravity.CENTER
            }.lparams(width = 0, height = 0) {
                topToBottom = R.id.guideline_h_10
                bottomToTop = R.id.guideline_h_20
                leftToRight = R.id.guideline_v_40
                rightToLeft = R.id.guideline_v_90
            }

            port_text_tv=editText {
                hint = "请输入端口"
                maxLines = 1
                id = R.id.port_text_tv
                inputType = InputType.TYPE_CLASS_NUMBER
                gravity = Gravity.LEFT + Gravity.CENTER
            }.lparams(width = 0, height = 0) {
                topToBottom = R.id.guideline_h_20
                bottomToTop = R.id.guideline_h_30
                leftToRight = R.id.guideline_v_40
                rightToLeft = R.id.guideline_v_90
            }

            save_bu_click=button {
                text = "保存"
                id = R.id.save_bu_click
            }.lparams(width = wrapContent, height = wrapContent) {
                topMargin = dip(40)
                leftToLeft = PARENT_ID
                rightToRight = PARENT_ID
                topToBottom = R.id.guideline_h_30
            }
        }
    }
}